package top.oylan.manager.tools;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import top.oylan.manager.entity.User;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-06-23
 */
public class ApiShiroRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        SimpleAuthenticationInfo info = null;

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String  account = token.getUsername();

        User user = new User();
        if(null == user){
            throw new UnknownAccountException();
        }else if(user.getType() != 1) {
            throw new DisabledAccountException();
        }else if(user.getStatus() != 1){
            throw new LockedAccountException();
        } else{
            info = new SimpleAuthenticationInfo(user,user.getPassword(),"apiShiroRealm");
        }
        return info;
    }
}
